import { Monitor } from "node-screenshots";
import { WebSocket } from "ws";

export class Task {
    clients: WebSocket[] = [];
    public is_running = false;
    public start(socket: WebSocket) {
        if (!this.clients.find(item => item == socket)) {
            this.clients.push(socket);
        }
        if (this.is_running == false) {
            this.is_running = true;
            setInterval(() => {
                this.capture();
            }, 200);
        }
    }


    private capture() {
        let monitors = Monitor.all();
        if (monitors && monitors.length) {
            let monitor = monitors[0];
            let img = monitor.captureImageSync();
            this.clients.forEach(client => {
                client.send(img.toJpegSync());
            })
        }

    }

}

